package com.iotimc.attendancesystem.attendance;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.base.BaseFragment;
import com.iotimc.attendancesystem.entity.PersonEntity;
import com.iotimc.attendancesystem.util.PictureManager;
import com.pefis.view.FaceDetectionView;
import com.pefis.view.FrameView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author david
 * @date 2017/11/27
 */

public class AttendanceFragment extends BaseFragment implements AttendanceContract.View {
    @BindView(R.id.faceDetectionView)
    FaceDetectionView mFaceDetectionView;
    @BindView(R.id.preview)
    ImageView mPreview;
    @BindView(R.id.iv_ver_img)
    ImageView mImg;
    AttendanceContract.Presenter presenter;
    @BindView(R.id.frameview)
    FrameView mFrameview;
    @BindView(R.id.tv_verification_position)
    TextView mTvVerificationPosition;
    @BindView(R.id.tv_verification_code)
    TextView mTvVerificationCode;
    @BindView(R.id.tv_verification_name)
    TextView mTvVerificationName;

    @BindView(R.id.ll_system_message)
    LinearLayout mLlVerificationMessage;
    private String mId = "";
    private boolean mStatus;

    @Override
    protected int getResId() {
        return R.layout.fragment_verification;
    }


    private boolean add;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        add = true;
    }
    int count = 0;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated: " + (count++));
    }

    public void setPresenter(AttendanceContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.i(TAG, "setUserVisibleHint: " + isVisibleToUser);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.i(TAG, "onHiddenChanged: " + hidden);

        if (!hidden) {
            PictureManager.getInstance().load(getContext(), R.drawable.test3, mPreview, false);
            presenter.resetCount();
            if (presenter != null) {
                presenter.setCheckAdmin(false);
                presenter.setmFaceDetectionView(this, mFaceDetectionView);
                if (!presenter.isRuning()) {
                    presenter.startAttendance();
                }
            }
            mLlVerificationMessage.setVisibility(View.INVISIBLE);
            mTvVerificationPosition.setText("");
        }
    }

    private static final String TAG = "AttendanceFragment";

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
        if (add) {
            add = false;
            onHiddenChanged(add);
        }
    }

    @Override
    public void resetUser() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mLlVerificationMessage.setVisibility(View.INVISIBLE);
            }
        });
    }

    @Override
    public void showImage(Bitmap bitmap) {
        if (getActivity() != null) {
            mPreview.setImageBitmap(bitmap);
        }
    }


    @Override
    public void showUser(final PersonEntity entity,boolean update) {
        //EventBus.getDefault().post(new AttendanceEvent(true));
        if (entity == null) {
            ((SystemActivity)getActivity()).setTime(1);
        }else {

            String entityId = entity.getId();

            if (!update) {
                return;
            }
            ((SystemActivity)getActivity()).setTime(15);
            mId = entityId;

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mLlVerificationMessage.setVisibility(View.VISIBLE);

                    //显示人员信息
                    //头像
                    byte[] image = entity.getImage();
                    if (image != null) {
                        PictureManager.getInstance().loadCirclePic(getActivity(), image, mImg, false);
                    } else {
                        mImg.setImageDrawable(
                                getActivity().getResources().getDrawable(R.drawable.error));
                    }

                    //职务
                    String position = entity.getPosition();
                    if (!TextUtils.isEmpty(position)) {
                        mTvVerificationPosition.setText(position);
                    }

                    //编号
                    String code = entity.getCode();
                    if (!TextUtils.isEmpty(code)) {
                        mTvVerificationCode.setText(code);
                    }

                    //姓名
                    String name = entity.getName();
                    if (!TextUtils.isEmpty(name)) {
                        mTvVerificationName.setText(name);
                    }

                }
            });

            Observable.timer(3, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                @Override
                public void accept(Long aLong) throws Exception {
                    mLlVerificationMessage.setVisibility(View.INVISIBLE);
                }
            });
        }

    }
}
